
package com.productmodule.dao;
import com.productmodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbProductDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbProduct tbProduct=null;
    private List<TbProduct> tbProductList=null;

    public boolean add(TbProduct inTbProduct){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Product(ProductTypeId,ProductName,InterfaceId,AreaId,UserId,ProductMemo,CreateTime) values(?,?,?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        pt.setInt(1,inTbProduct.getProductTypeId());
        pt.setString(2,inTbProduct.getProductName());
        pt.setString(3,inTbProduct.getInterfaceId());
        pt.setString(4,inTbProduct.getAreaId());
        pt.setInt(5,inTbProduct.getUserId());
        pt.setString(6,inTbProduct.getProductMemo());
        pt.setString(7,inTbProduct.getCreateTime());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public boolean del(int inProductId){
        return UtilDAO.del("Tb_Product", "ProductId", inProductId);
    }
    public boolean edit(TbProduct inTbProduct){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Product set ProductId=?,ProductTypeId=?,ProductName=?,InterfaceId=?,AreaId=?,UserId=?,ProductMemo=?,CreateTime=? where ProductId=?";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbProduct.getProductId());
        pt.setInt(2,inTbProduct.getProductTypeId());
        pt.setString(3,inTbProduct.getProductName());
        pt.setString(4,inTbProduct.getInterfaceId());
        pt.setString(5,inTbProduct.getAreaId());
        pt.setInt(6,inTbProduct.getUserId());
        pt.setString(7,inTbProduct.getProductMemo());
        pt.setString(8,inTbProduct.getCreateTime());
        pt.setInt(9,inTbProduct.getProductId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public TbProduct getTbProductByProductId(int inProductId) {
        tbProduct=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Product where ProductId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inProductId);
            rs = pt.executeQuery();
            if (rs.next()) {
                if (tbProduct == null)tbProduct = getTbProduct(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbProduct;
    }
    public List<TbProduct> getTbProductAll() {
        tbProductList=new ArrayList<TbProduct>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Product";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbProductList.add(getTbProduct(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbProductList;
    }
    public List<TbProduct> getTbProductAll(VskPage vp) {
        tbProductList=new ArrayList<TbProduct>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Product";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Product", "ProductId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbProductList.add(getTbProduct(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbProductList;
    }
    public TbProduct getTbProduct(ResultSet rs) throws Exception{
        TbProduct tbProduct = new TbProduct();
        tbProduct.setProductId(rs.getInt("ProductId"));
        tbProduct.setProductTypeId(rs.getInt("ProductTypeId"));
        tbProduct.setProductName(rs.getString("ProductName"));
        tbProduct.setInterfaceId(rs.getString("InterfaceId"));
        tbProduct.setAreaId(rs.getString("AreaId"));
        tbProduct.setUserId(rs.getInt("UserId"));
        tbProduct.setProductMemo(rs.getString("ProductMemo"));
        tbProduct.setCreateTime(rs.getString("CreateTime"));
        return tbProduct;
    }
}
